package org.json.demo;

import junit.framework.Assert;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

public class SaveDemo {
	public static final int SIZE = 100;
	
	private static JSONArray persons = new JSONArray();
	
	private static String data  = "";
	
	@Test
	public void testMaker() {
		try {
			for (int i = 0; i < SIZE; i++) {
				JSONObject json = new JSONObject();
				json.put("id", i);
				json.put("name", "Mike");
				json.put("sex", "男");
				json.put("email", "zhaoming23@gmail.com");
				persons.put(json);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		data = persons.toString();
		System.out.println(persons.toString());
	}
	
	@Test
	public void testRead() {
		try {
			JSONArray persons = new JSONArray(data);
			Assert.assertEquals(SIZE, persons.length());
			for (int i = 0; i < persons.length(); i++) {
				JSONObject object = persons.getJSONObject(i);
				System.out.println(object.get("id"));
				System.out.println(object.get("name"));
				System.out.println(object.get("sex"));
				System.out.println(object.get("email"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
